package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.modules.sxjy.common.noresubmit.CacheLock;
import org.edu.modules.sxjy.entity.VeEstate;
import org.edu.modules.sxjy.service.CacheKeyGenerator;
import org.edu.modules.sxjy.service.IVeEstateService;
import org.edu.modules.sxjy.service.impl.CacheKeyGeneratorImp;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.utils.PagingUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Api(tags={"产业信息管理"})
@RestController
@RequestMapping({"/sxjy/veEstate"})
@ApiSort(60)
public class VeEstateController
        extends BaseController<VeEstate, IVeEstateService>
{
    private static final Logger log = LoggerFactory.getLogger(VeEstateController.class);
    @Autowired
    private IVeEstateService veEstateService;

    @Bean
    public CacheKeyGenerator cacheKeyGenerator()
    {
        return new CacheKeyGeneratorImp();
    }

    @AutoLog("产业信息管理-导入数据")
    @ApiOperation(value="导入数据", notes="产业信息管理-导入数据")
    @RequestMapping(value={"/importExcel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response)
    {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        Iterator localIterator = fileMap.entrySet().iterator();
        if (localIterator.hasNext())
        {
            Map.Entry<String, MultipartFile> entity = (Map.Entry)localIterator.next();
            MultipartFile file = (MultipartFile)entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try
            {
                List<VeEstate> listSysCategorys = ExcelImportUtil.importExcel(file.getInputStream(), VeEstate.class, params);
                //lbh 2024/07/31 localObject1 >> Iterator localObject1
                for (Iterator localObject1 = listSysCategorys.iterator(); ((Iterator)localObject1).hasNext();)
                {
                    VeEstate veEstate = (VeEstate)((Iterator)localObject1).next();
                    veEstate.setTerminalId(1);
                    this.veEstateService.save(veEstate);
                }
                return Result.ok("文件导入成功！数据行数：" + listSysCategorys.size());
            }
            catch (Exception e)
            {
                Object localObject1;
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败：" + e.getMessage());
            }
            finally
            {
                try
                {
                    file.getInputStream().close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    @AutoLog("产业信息管理-查询列表")
    @ApiOperation(value="查询列表", notes="产业信息管理-查询列表")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectAll(VeEstate veEstate, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize)
    {
        List<VeEstate> veEstateList = this.veEstateService.selectAllList(veEstate);
        Map<String, Object> pageData = PagingUtils.getPageData(veEstateList, pageNo, pageSize);
        List<VeEstate> veEstateLists = (List)pageData.get("data");
        int totalPage = ((Integer)pageData.get("totalPage")).intValue();
        return new PageResponseResult(pageNo, totalPage, veEstateList.size(), Result.OK(veEstateLists));
    }

    @AutoLog("产业信息管理-通过id查询")
    @ApiOperation(value="通过id查询", notes="产业信息管理-通过id查询")
    @GetMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeEstate veEstate = (VeEstate)this.veEstateService.getById(id);
        if (veEstate == null) {
            return Result.OK();
        }
        return Result.OK(veEstate);
    }

    @AutoLog("产业信息管理-新增信息")
    @ApiOperation(value="新增信息", notes="产业信息管理-新增信息")
    @PostMapping({"/add"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> add(@RequestBody VeEstate veEstate)
    {
        if (veEstate != null)
        {
            this.veEstateService.save(veEstate);
            return Result.OK("添加成功！");
        }
        return Result.error("添加失败");
    }

    @AutoLog("产业信息管理-修改信息")
    @ApiOperation(value="修改信息", notes="产业信息管理-修改信息")
    @PutMapping({"/edit"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> edit(@RequestBody VeEstate veEstate)
    {
        if (veEstate != null)
        {
            this.veEstateService.updateById(veEstate);
            return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败");
    }

    @AutoLog("产业信息管理-通过id删除")
    @ApiOperation(value="通过id删除", notes="产业信息管理-通过id删除")
    @DeleteMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veEstateService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("产业信息管理-批量删除")
    @ApiOperation(value="批量删除", notes="产业信息管理-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veEstateService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
}
